package motor;

import utils.Vector2d;
import java.util.*;
import java.awt.*;

import modele.Noeud;

/**
 * A simple class to handle the surface itself. All objects are polylines.
 * 
 * @author L. Simon, Univ. Paris Sud, 2008.
 *
 */
public class Surface
{
	public int wxsize, wysize;

	// All objects on the surface are recorded in this vector of polylines.
	public Vector<PolylineObject> objects; // The objects on the surface

	/**
	 * Well, right now the objects are built "by hands". May by the first
	 * thing to do would be to put polylines objects in a map, and read the
	 * file and objects.
	 * 
	 * In this case, the size of the surface should be set inside the constructor?
	 * 
	 * @param wxsize
	 * @param wysize
	 * @param scale
	 */
	public Surface( int wxsize, int wysize, float scale)
	{
		this.wxsize = wxsize;
		this.wysize = wysize;
		objects = new Vector<PolylineObject>();
		
		float cadre = 4;
		
		PolylineObject ob1 = new PolylineObject(new Vector2d(0,0),this);
		ob1.addNode(new Vector2d(cadre,0));
		ob1.addNode(new Vector2d(cadre,wysize));
		ob1.addNode(new Vector2d(0,wysize));
		ob1.fixObject();
		objects.add(ob1);
		
		PolylineObject ob2 = new PolylineObject(new Vector2d(0,wysize),this);
		ob2.addNode(new Vector2d(0,wysize-cadre));
		ob2.addNode(new Vector2d(wxsize,wysize-cadre));
		ob2.addNode(new Vector2d(wxsize,wysize));
		ob2.fixObject();
		objects.add(ob2);
		
		PolylineObject ob3 = new PolylineObject(new Vector2d(wxsize,0),this);
		ob3.addNode(new Vector2d(wxsize-cadre,0));
		ob3.addNode(new Vector2d(wxsize-cadre,wysize));
		ob3.addNode(new Vector2d(wxsize,wysize));
		ob3.fixObject();
		objects.add(ob3);
		
		PolylineObject ob4 = new PolylineObject(new Vector2d(0,0),this);
		ob4.addNode(new Vector2d(0,cadre));
		ob4.addNode(new Vector2d(wxsize,cadre));
		ob4.addNode(new Vector2d(wxsize,0));
		ob4.fixObject();
		objects.add(ob4);
		
		PolylineObject ob5 = new PolylineObject(new Vector2d(300F,50F),this);
		ob5.addNode(new Vector2d(300F,80F));
		ob5.addNode(new Vector2d(320F,80F));
		ob5.addNode(new Vector2d(320F,50F));
		ob5.fixObject();
		objects.add(ob5);

		/*
		PolylineObject ob1 = new PolylineObject(new Vector2d(100F,200F),this);
		ob1.addNode(new Vector2d(100F,250F));
		ob1.addNode(new Vector2d(200F,250F));
		ob1.addNode(new Vector2d(200F,200F));
		ob1.fixObject();
		objects.add(ob1);

		PolylineObject ob2 = new PolylineObject(new Vector2d(300F,500F),this);
		ob2.addNode(new Vector2d(300F,800F));
		ob2.addNode(new Vector2d(320F,800F));
		ob2.addNode(new Vector2d(320F,500F));
		ob2.fixObject();
		objects.add(ob2);
		
		PolylineObject ob3 = new PolylineObject(new Vector2d(300F,50F),this);
		ob3.addNode(new Vector2d(300F,80F));
		ob3.addNode(new Vector2d(320F,80F));
		ob3.addNode(new Vector2d(320F,50F));
		ob3.fixObject();
		objects.add(ob3);
		*/
		/*
		int nbRec = 30;
		int tXmax = 150;
		int tYmax = 150;
		int min = 3;
		
		for( int i = 0 ; i < nbRec ; i++ )
		{
			float tX = (int) (Math.random() * tXmax) + min;
			float tY = (int) (Math.random() * tYmax) + min;
			
			float depX = (int) (Math.random() * ( this.wxsize - tX ));
			float depY = (int) (Math.random() * ( this.wysize - tY ));
			
			//System.out.println("Ty : " + tY + " Tx : " + tX );
			//System.out.println("depY : " + depY + " depX : " + depX );
			
			PolylineObject ob = new PolylineObject(new Vector2d(depX,depY),this);
			ob.addNode(new Vector2d(depX+tX,depY));
			ob.addNode(new Vector2d(depX+tX,depY+tY));
			ob.addNode(new Vector2d(depX,depY+tY));
			ob.fixObject();
			objects.add(ob);
		}*/
		
		/*
		PolylineObject ob4 = new PolylineObject(new Vector2d(500F,200F),this);
		
		float tot = 7;
		float r = 100;
		
		for( int i = 1 ; i < tot ; i++ )
		{
			ob4.addNode(new Vector2d((float) (500F + r * Math.cos( i/tot * 2 * Math.PI )),(float) (200F + r * Math.sin( i/tot * 2 * Math.PI ))));
		}
		ob4.fixObject();
		objects.add(ob4);
		
		

		System.out.println("Taille object : " + ob4.globalCoordPoints.size());
	*/
	
	}

	public void draw(Graphics g)
	{
		g.setColor(Color.BLACK);
		for(int i=0;i<objects.size();i++)
		{
			objects.get(i).draw(g);
		}
	}

	/**
	 * One of the main methods. Checks if the segment (tmpA, tmpB)
	 * intersects any of the lines of any polyline. If not, then
	 * the point tmpA 'can see' the point tmpB.
	 * 
	 * @param tmpA
	 * @param tmpB
	 * @return true if tmpA can see tmpB
	 */
	public Vector2d cansee(Vector2d tmpA, Vector2d tmpB)
	{
		for(int i=0;i<objects.size();i++)
		{
			Vector2d res = objects.get(i).intersectsWith(tmpA, tmpB);
			if ( res != null )
				return res;
		}
		return null;
	}
}

